package com.mrd.util;

import com.mrd.util.date.DateUtil;

import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * application name: sample
 * <p>
 * copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * 生成随机值的类，通常用来产生主键
 * 
 * @author qiul date : 2012-5-29上午9:34:20
 */
public class KeyGenerator {

  /**
   * 根据前后缀生成随机字符串
   * 
   * @param prefix 前缀
   * @param suffix 后缀
   * @return 前缀+32位随机字符串+后缀
   */
  public static String getKey(String prefix, String suffix) {
    if (prefix == null)
      prefix = "";
    if (suffix == null)
      prefix = "";
    String uid = UUID.randomUUID().toString();
    uid = uid.replaceAll("-", "");
    return prefix + uid + suffix;
  }

  /**
   * 根据前缀生成随机字符串
   * 
   * @param prefix 前缀
   * @return 前缀+32位随机字符串
   */
  public static String getKey(String prefix) {
    return getKey(prefix, "");
  }

  /**
   * 生成32位随机字符串
   * 
   * @return
   */
  public static String getKey() {
    return getKey("", "");
  }

  /** The FieldPosition. */
  private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

  private static final String defaultPattern = "yyyyMMdd";

  /** This Format for format the number to special format. */
  private final static NumberFormat numberFormat = new DecimalFormat("000");

  /** This int is the sequence number ,the default value is 0. */
  private static int seq = 0;

  private static final int MAX = 999;

  /**
   * 按时间格式生成id
   * 
   * @return String
   */
  public static synchronized String generateSequenceNo(String prefix, String pattern) {
    if (StringUtils.isEmpty(pattern)) {
      pattern = defaultPattern;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    Calendar rightNow = Calendar.getInstance();

    StringBuffer sb = new StringBuffer();

    dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);

    numberFormat.format(seq, sb, HELPER_POSITION);

    if (seq == MAX) {
      seq = 0;
    } else {
      seq++;
    }
    return prefix + sb.toString();
  }

  public static synchronized String generateSequenceNo(String prefix) {
    return prefix + generateSequenceNo(prefix, "yyyyMMdd");
  }

  public static synchronized String generateRadomSequenceNo(String prefix) {
    return generateSequenceNo(prefix, "yyyyMMddhhmmssSSS");
  }

  public static synchronized String generateSequenceNo() {
    return generateSequenceNo("");
  }

  /**
   * 按时间格式生成id
   * 
   * @param prefix
   * @param pattern
   * @return prefix+按照pattern格式化后的当前时间
   */
  public static synchronized String generateNoFromDate(String prefix, String pattern) {
    if (StringUtils.isEmpty(pattern)) {
      pattern = defaultPattern;
    }
    return prefix + DateUtil.formatDate(new Date(), pattern);
  }

  public static synchronized String generateNoFromDate(String pattern) {
    return generateNoFromDate("", pattern);
  }

  private static int getRandom(int count) {
    return (int) Math.round(Math.random() * (count));
  }

  private static String string = "abcdefghijklmnopqrstuvwxyz";

  private static String getRandomString(int length) {
    StringBuffer sb = new StringBuffer();
    int len = string.length();
    for (int i = 0; i < length; i++) {
      sb.append(string.charAt(getRandom(len - 1)));
    }
    return sb.toString();
  }
}
